/*
 * UrlActivity.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.url;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseActivity;
import com.yidiandao.business.constant.Intents;
import com.yidiandao.business.utils.ActivityUtils;

/**
 * 项目WebView页面
 */
public class UrlActivity extends BaseActivity {

    private UrlFragment mUrlFragment;


    /**
     * 普通跳转H5
     * <p>
     * <b>具备分享功能</b>
     */
    public static void actionStart(Activity activity, String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }

        Intent intent = new Intent(activity, UrlActivity.class);
        intent.putExtra(Intents.HTTP_URL, url);
        intent.putExtra(Intents.EXTRA_ENABLE_SHARE_FEATURE, true);
        activity.startActivity(intent);
    }

    /**
     * 不具备分享功能的H5跳转.
     */
    public static void actionStartWithoutShareFeature(Activity activity, String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }

        Intent intent = new Intent(activity, UrlActivity.class);
        intent.putExtra(Intents.HTTP_URL, url);
        activity.startActivity(intent);
    }

    /**
     * 支持横竖屏切换的H5跳转.
     * <b>主要在查看配送订单所用</b>
     */
    public static void actionStartFreeScreenOrientation(Activity activity, String url) {

        if (TextUtils.isEmpty(url)) {
            return;
        }

        Intent intent = new Intent(activity, UrlActivity.class);
        intent.putExtra(Intents.FREE_SCREEN_ORIENTATION, true);
        intent.putExtra(Intents.HTTP_URL, url);
        intent.putExtra(Intents.EXTRA_ENABLE_SHARE_FEATURE, true);
        activity.startActivity(intent);
    }

    /**
     * 跳转快钱-在线支付H5
     * <p>
     * 普通的在线支付跳转只会调用成功的交互,不会有其他额外的动作,比如点返回键时查看订单的状态
     * <p>
     * <b>在线支付不会有分享功能</b>
     */
    public static void actionStartForOnlinePay(Activity activity, String url, String orderPayId, boolean isFirstIn) {
        if (TextUtils.isEmpty(url)) {
            return;
        }

        Intent intent = new Intent(activity, UrlActivity.class);
        intent.putExtra(Intents.HTTP_URL, url);
        intent.putExtra(Intents.EXTRA_FOR_ONLINE_PAY, true);
        intent.putExtra(Intents.EXTRA_FIRST_IN, isFirstIn);
        intent.putExtra(Intents.EXTRA_ORDER_PAY_ID, orderPayId);
        activity.startActivity(intent);
    }


    /**
     * 报名活动-在线支付.返回时会检查在线支付的结果,并根据结果进行跳转.
     * <pre>
     * 如果支付失败,则会跳转支付失败页面
     * 如果支付成功,则会告知报名页面支付成功
     * 每次点击返回键都会调用接口检查支付的状态
     * </pre>
     * <b>在线支付不会有分享功能</b>
     */
    public static void actionStartForEnrollOnlinePay(Activity activity, String url, String actId) {
        Intent intent = new Intent(activity, UrlActivity.class);
        intent.putExtra(Intents.HTTP_URL, url);
        intent.putExtra(Intents.EXTRA_FIRST_IN, true);
        intent.putExtra(Intents.EXTRA_FOR_ONLINE_PAY, true);
        intent.putExtra(Intents.EXTRA_REC_ID, actId);
        activity.startActivity(intent);
    }


    /**
     * 进入文章详情
     * <pre>
     *     文章详情没有Toolbar;
     *     文章详情底部有点赞,分享与跳转评论的按钮
     * </pre>
     */
    public static void actionStartForArticle(final Activity activity, String articleId) {
        Intent intent = new Intent(activity, UrlActivity.class);
        intent.putExtra(Intents.EXTRA_ARTICLE, true);
        intent.putExtra(Intents.EXTRA_ARTICLE_ID, articleId);
        activity.startActivity(intent);
    }


    @Override
    protected void initFragment() {
        mUrlFragment = (UrlFragment) getSupportFragmentManager().findFragmentById(R.id.container_url);
        if (mUrlFragment == null) {
            mUrlFragment = UrlFragment.getInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(), mUrlFragment, R.id.container_url);
        }
    }


    @Override
    protected void initPresenter() {
        new UrlPresenter(mUrlFragment, getIntent().getExtras());
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_url;
    }


    @Override
    public void onBackPressed() {
        if (mUrlFragment.canGoBack()) {
            mUrlFragment.goBack();
        } else {
            mUrlFragment.onBackPress();
            super.onBackPressed();
        }
    }
}
